package cn.edu.jnu.x2020101361;


public class BowlingGame {
    private int[] rolls = new int[21];
    private int rollIndex = 0;

    public void roll(int i) {
        rolls[rollIndex++] = i;
    }

    public int score() {
        int score = 0;
        int frameIndex = 0;
        for(int frame=0; frame<10; frame++) {
            if (isSpare(frameIndex)) {
                score += 10 + rolls[frameIndex + 2];
                frameIndex += 2;
            }
            else if (isStrike(rolls[frameIndex])) {
                score += 10 + rolls[frameIndex+1] + rolls[frameIndex+2];
                frameIndex += 1;
            }
            else {
                score += rolls[frameIndex] + rolls[frameIndex+1];
                frameIndex += 2;
            }
        }
        return score;
    }

    private boolean isStrike(int roll) {
        return 10 == roll;
    }

    private boolean isSpare(int frameIndex) {
        return 10 == (rolls[frameIndex] + rolls[frameIndex +1]);
    }
}
